<?php

namespace App\Http\Controllers\User;

use App\Models\UserAttribute;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Models\Users;
use App\Models\UserGrowing;
use App\Models\TeacherTransferLog;
use App\Models\ActivityTeam;
use App\Models\ActivityShareRecords;
use App\Models\ActivityUserStatistics;
use App\User;
use App\Models\EnrollTeacher;
use Illuminate\Support\Facades\DB;
use Excel;
class UserController extends Controller
{
	protected $ret;

	public function __construct()
    {
    	$this->ret = [];
    }
    //20180718  banner列表
    public function index(){

    	$where= array();
    	if (Input::has('id')){
    		$where['id'] = Input::get('id');
		}
		if (Input::has('name')){
    		$where['name'] = Input::get('name');
		}
		if (Input::has('status')){
    		$where['status'] = Input::get('status');
		}
		if (Input::has('mobile')){
    		$where['mobile'] = Input::get('mobile');
		}
		$list = DB::table("users")->where($where)->where('is_admin',0)->orderBy("id", "desc")->paginate(10);
    	$this->ret['list'] = $list;
    	$this->ret['id']   = Input::get('id');
    	$this->ret['name'] = Input::get('name');
    	$this->ret['status'] = Input::get('status');

        return view('user.index', $this->ret);
    }

    //20180718  修改用户状态
    public function upUserStatus(){
    	$id     = Input::get("id");
    	$status = Input::get("status");
    	$data['status'] = $status;
		$re = DB::table("users")->where("id",$id)->update($data);
		if($re){
			return json_encode(['code'=>1]);
		}else{
			return json_encode(['code'=>0, 'msg'=>'参数错误']);
		}
    }

    //20180718  查看用户详情默认为课程页面
    public function userDetail($user_id){
    	$list = DB::table("course_class")
    	->leftJoin("course_type" , 'course_class.course_type_id', '=', 'course_type.id')
    	->leftJoin("users" , 'course_class.opertion_user_id', '=', 'users.id')
    	->select("course_class.*", "course_type.title as cytitle","users.name")
    	->where("course_class.user_id", '=', $user_id)
    	->paginate(5);
    	$user = DB::table("users")->where("id", $user_id)->get();
    	
    	
    	$this->ret['list']    = $list;
    	$this->ret['user_id'] = $user_id; 
    	$this->ret['user']    = $user[0];
    	return view('user.detail', $this->ret);
    }

    //20180718  查看用户详情默认为课程页面
    public function userDetailArticle($user_id){
    	$list = DB::table("course_class")
    	->leftJoin("course_type" , 'course_class.course_type_id', '=', 'course_type.id')
    	->leftJoin("users" , 'course_class.opertion_user_id', '=', 'users.id')
    	->select("course_class.*", "course_type.title as cytitle","users.name")
    	->where("course_class.user_id", '=', $user_id)
    	->paginate(5);
    	
    	
    	$this->ret['list']    = $list;
    	$this->ret['user_id'] = $user_id; 
    	return view('user.detailarticle', $this->ret);
    }

    //20180813  查看用户详情账户流水
    public function userDetailFinanceRecord($user_id){
        $list = DB::table("finance_records")
        ->leftJoin("course_class" , 'finance_records.course_class_id', '=', 'course_class.id')
        ->select("finance_records.*", "course_class.title")
        ->where("finance_records.user_id", '=', $user_id)
        ->paginate(5);
        $user = DB::table("users")->where("id", $user_id)->get();

        $this->ret['list']    = $list;
        $this->ret['user_id'] = $user_id; 
        $this->ret['user']    = $user[0];
        return view('user.financerecord', $this->ret);
    }

    //20180813  查看用户详情账户流水
    public function userDetailComment($user_id){
        $list = DB::table("comments")
        ->leftJoin("course_class" , 'comments.course_class_id', '=', 'course_class.id')
        ->select("comments.*", "course_class.title")
        ->where("comments.user_id", '=', $user_id)
        ->paginate(5);
        $user = DB::table("users")->where("id", $user_id)->get();

        $this->ret['list']    = $list;
        $this->ret['user_id'] = $user_id; 
        $this->ret['user']    = $user[0];
        return view('user.comment', $this->ret);
    }
    public function spb(Request $request,$user_id){
        if($request->user()){
            $operator = $request->user()->id;
        }else{
            $operator = 0;
        }
       $re = courseSpb($user_id,14,"0","0",$operator);
       //dd($re);
       if($re){
            echo "<script>window.location.href = document.referrer;</script>";
       }else{
            echo "<script>window.location.href = document.referrer;</script>";
       }

    }

	/*
	 * 招生老师
	 */
	public function zsTeacher(Request $request){
		$name = $request->input('name','');
		$teachers = new EnrollTeacher();
		if($name){
			$teachers = $teachers->where('name','like','%'.$name.'%');
		}
		$teachers = $teachers->select('id','name','mobile','openid')->orderBy('id','desc')->paginate(10);
		return view('user.zsTeacher',['teachers'=>$teachers,'name'=>$name]);
	}

	/*
	 * 导入招生老师信息
	 */
	public function exportTeacher(Request $request){
		$filePath = public_path().'/教师信息.xls';
        
		Excel::load($filePath, function($reader) {
			$data = $reader->all();
			$enrollTeacher = new EnrollTeacher();
			foreach($data as $teachers){
				foreach($teachers  as $teach){
					$teacher = EnrollTeacher::where('mobile',$teach->mobile)->select('id')->first();
					if($teacher){
						continue;
					}
					$enrollTeacher = new EnrollTeacher();
					$enrollTeacher->name = $teach->employee_name;
					$enrollTeacher->mobile = $teach->mobile;
					$enrollTeacher->save();
				}
			}
		});
		$this->getSessionInfo($request,'success','导入成功');
		return redirect('/user/enroll');
	}

	public function userInfo(Request $request){
		$mobile = $request->input('mobile');
		$user = User::where('mobile',$mobile)->first();
		if($user){
			$attribute = $user->userAttribute;
			if(!$attribute->is_fission){
				return $this->getMessage(1,'不是裂变者');
			}else{
				return $this->getMessage(0,'成功',['name'=>$user->nickname,'id'=>$user->id]);
			}
		}else{
			return $this->getMessage(1,'用户不存在');
		}

	}
    /*
     * 交接老师
     */
    public function transferTeacher(Request $request){
        $mobile = trim($request->input('mobile'));
        $user_id = $request->input('user_id');
        $user = User::where('id',$user_id)->first();
        if(!$user){
            return $this->getMessage(1,'用户不存在');
        }
        DB::beginTransaction();
        try{
            $transUser = User::where('mobile',$mobile)->first();
            $userGrows = UserGrowing::where('fission_id',$user_id)->select('id','user_id')->get();
            $teacherTrans = new TeacherTransferLog();
            $teacherTrans->user_id = $user_id;
            $teacherTrans->transfer_user_id = $transUser->id;
            $teacherTrans->content = json_encode($userGrows);
            $teacherTrans->save();
            UserGrowing::where('fission_id',$user_id)->update(['fission_id'=>$transUser->id]);
            DB::commit();
            return $this->getMessage(0,'移交成功');
        }catch(\Exception $e){
            DB::rollback();
            return $this->getMessage(1,$e->getMessage());
        }
    }


	public function active1(Request $request)
	{
		$name = $request->input('name','');
		$mobile = $request->input('mobile','');
		$id = $request->input('id','');
		$userActives = new ActivityTeam();
		if($id){
			$userActives = $userActives->where('userid',$id);
		}
		if($mobile){
			$users = User::where('mobile','like','%'.$mobile.'%')->select('id')->get();
			$userArr = [];
			foreach($users as $user){
				$userArr[] = $user->id;
			}
			if(!empty($userArr)){
				$userActives = $userActives->whereIn('userid',$userArr);
			}
		}
		if($name){
			$users = User::where('name','like','%'.$name.'%')->orWhere('nickname','like','%'.$name.'%')->select('id')->get();
			$userArr = [];
			foreach($users as $user){
				$userArr[] = $user->id;
			}
			if(!empty($userArr)){
				$userActives = $userActives->whereIn('userid',$userArr);
			}
		}
		$userActives = $userActives->where('is_get', 1)->where('is_sponsor', 1)->select('userid')->groupBy('userid')->paginate(10);

		$data['userActives'] = $userActives;
		$data['mobile'] = $mobile;
		$data['name'] = $name;

		$data['id'] = $id;
		return view('user.userActive',$data);
	}

	public function active(Request $request){
		$name = $request->input('name','');
		$mobile = $request->input('mobile','');
		$id = $request->input('id','');
		$userStatic = new ActivityUserStatistics();
		if($id){
			$userStatic = $userStatic->where('user_id',$id);
		}
		if($mobile){
			$users = User::where('mobile','like','%'.$mobile.'%')->select('id')->get();
			$userArr = [];
			foreach($users as $user){
				$userArr[] = $user->id;
			}
			if(!empty($userArr)){
				$userStatic = $userStatic->whereIn('user_id',$userArr);
			}
		}
		if($name){
			$users = User::where('name','like','%'.$name.'%')->orWhere('nickname','like','%'.$name.'%')->select('id')->get();
			$userArr = [];
			foreach($users as $user){
				$userArr[] = $user->id;
			}
			if(!empty($userArr)){
				$userStatic = $userStatic->whereIn('user_id',$userArr);
			}
		}
		$userStatic = $userStatic->select('user_id')->paginate(10);

		$data['userStatic'] = $userStatic;
		$data['mobile'] = $mobile;
		$data['name'] = $name;

		$data['id'] = $id;
		return view('user.userActiveStatic',$data);
	}
}
